package org.example.iris.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.example.iris.entity.Job;
import org.example.iris.dto.JobInfo;

import java.util.List;

@Mapper
public interface JobMapper {

    /**
     * 插入一个新的职位信息
     * @param job 职位对象
     * @return 插入的记录数
     */
    @Insert("INSERT INTO job (company_id, title, description, salary, requirement, publish_date, status, area) " +
            "VALUES (#{companyId}, #{title}, #{description}, #{salary}, #{requirement}, #{publishDate}, #{status}, #{area})")
    int insertJob(Job job);

    @Select("select * from job where job_id = #{id}")
    Job findById(Integer id);

    @Select("select job.company_id, job_id, company_name, title, job.description, salary, requirement, publish_date, area " +
            "from job JOIN company ON job.company_id = company.company_id " +
            "where job.job_id = #{jobId}")
    JobInfo getJobInfoById(Integer jobId);

    @Select("select job.company_id, job_id, company_name, title, job.description, salary, requirement, publish_date, area " +
            "from job JOIN company ON job.company_id = company.company_id " +
            "where (job.title like concat('%', #{keyword}, '%') " +
            "or job.description like concat('%', #{keyword}, '%')) " +
            "and job.status = 'published'" +
            "and area like concat('%', #{city}, '%')")
    List<JobInfo> selectJob(String keyword, String city);

    /**
     * 根据公司ID获取职位信息
     * @param companyId 公司ID
     * @return 职位列表
     */
    @Select("SELECT * FROM job WHERE company_id = #{companyId}")
    List<Job> getJobsByCompanyId(Integer companyId);

    @Select("select job.company_id, job_id, company_name, title, job.description, salary, requirement, publish_date, area " +
            "from job JOIN company ON job.company_id = company.company_id " +
            "where job.company_id = #{companyId}")
    List<JobInfo> selectByCompanyId(Integer companyId);

    @Select("select job.company_id, job_id, company_name, title, job.description, salary, requirement, publish_date, area " +
            "from job JOIN company ON job.company_id = company.company_id " +
            "and job.status = 'published'" +
            "ORDER BY RAND() LIMIT 6")
    List<JobInfo> recommendJob();

    @Select("select job.company_id, job_id, company_name, title, job.description, salary, requirement, publish_date, area " +
            "from job JOIN company ON job.company_id = company.company_id " +
            "where job.company_id = #{companyId} " +
            "and job.status = 'published'" +
            "ORDER BY RAND() LIMIT 3")
    List<JobInfo> selectThreeByCompanyId(Integer companyId);
}
